package com.meet.controller;

/**
 * @author RicardoZeng
 * @date 2022/8/11 21:22
 */

import com.meet.dao.BoardRoomDao;
import com.meet.dao.BoardRoomDaoImpl;
import com.meet.dao.MeetingDao;
import com.meet.dao.MeetingDaoImpl;
import com.meet.entity.BoardRoom;
import com.meet.entity.Meeting;
import com.meet.entity.PageBean;
import com.meet.service.BoardRoomService;
import com.meet.service.BoardRoomServiceImpl;
import com.meet.service.MeetingService;
import com.meet.service.MeetingServiceImpl;
import com.meet.util.DateUtil;
import com.meet.vo.BoardRoomTimeVo;


import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

@WebServlet("/servlet/BoardroomServlet")
public class BoardRoomServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BoardRoom boardRoom = null;
        BoardRoomDao boardRoomDao = new BoardRoomDaoImpl();
        BoardRoomService boardRoomService = new BoardRoomServiceImpl(boardRoomDao);
        MeetingDao meetingDao=new MeetingDaoImpl();
        MeetingService meetingService=new MeetingServiceImpl(meetingDao);
//
//        UserDao userDao = new UserDaoImpl();
//        UserService userService = new UserServiceImpl(userDao);
//        HttpSession session = request.getSession();
//        //判断是否有权限：
//        User user = (User) session.getAttribute("user");

        String flag = request.getParameter("flag");

        if ("1".equals(flag)) {
            //1.获取参数
            String currentPage = request.getParameter("page");//当前页码
            String rows = request.getParameter("rows");//每页显示条数

            if (currentPage == null || "".equals(currentPage)) {

                currentPage = "1";
            }

            if (rows == null || "".equals(rows)) {
                rows = "7";
            }
            PageBean<BoardRoom> pb = null;

            try {
                pb = boardRoomService.getAllRoomsByPage(currentPage, rows);
                System.out.println(pb);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            //3.将PageBean存入request
            request.setAttribute("pb", pb);

            //4.转发到main.jsp
            request.getRequestDispatcher("boardroomAll.jsp").forward(request, response);

        } else if ("2".equals(flag)) {

            String boardRoomName = request.getParameter("rName");
            Integer number = Integer.valueOf(request.getParameter("number"));
            String s = request.getParameter("state");
            Boolean state = false;
            if ("启用".equals(s)) {
                state = false;
            } else {
                state = true;
            }

            BoardRoom boardRooms = new BoardRoom(null, boardRoomName, number, state);
            try {
                int count = boardRoomService.addBoardRoom(boardRooms);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            request.getRequestDispatcher("BoardroomServlet?flag=1&currentPage=1").forward(request, response);

        } else if ("3".equals(flag)) {

            Integer id = Integer.valueOf(request.getParameter("id"));

            try {
                boardRoom = boardRoomService.getRoom(id);
                request.setAttribute("boardRoom", boardRoom);
                request.getRequestDispatcher("boardroomUpdate.jsp").forward(request, response);

            } catch (SQLException e) {
                e.printStackTrace();
            }

        } else if ("4".equals(flag)) {

            Integer id =Integer.valueOf(request.getParameter("id"));
            String boardRoomName = request.getParameter("rName");
            Integer number = Integer.valueOf(request.getParameter("number"));
            String s = request.getParameter("state");
            Boolean state = true;
            if ("启用".equals(s)) {
                state = false;
            } else {
                state = true;
            }
            BoardRoom boardRooms = new BoardRoom(id, boardRoomName, number, state);

            try {
                boardRoomService.updateBoardRoom(boardRooms);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            request.getRequestDispatcher("BoardroomServlet?flag=1").forward(request, response);

        } else if ("5".equals(flag)) {

            Integer id = Integer.valueOf(request.getParameter("id"));

            try {
                boardRoomService.deleteBoardRoom(id);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            request.getRequestDispatcher("BoardroomServlet?flag=1").forward(request, response);

        }else if("6".equals(flag)){ //预定会议功能调往会议室页面
            //1.获取参数
            String currentPage = request.getParameter("currentPage");//当前页码
            String rows = request.getParameter("rows");//每页显示条数

            if (currentPage == null || "".equals(currentPage)) {

                currentPage = "1";
            }

            if (rows == null || "".equals(rows)) {
                rows = "10";
            }
            PageBean<BoardRoom> pb = null;

            try {
                pb = boardRoomService.getAllRoomsByPage(currentPage, rows);
                System.out.println(pb);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            //3.将PageBean存入request
            request.setAttribute("pb", pb);

            //4.转发到main.jsp
            request.getRequestDispatcher("boardroomAll1.jsp").forward(request, response);
        }else if("7".equals(flag)){ //预定会议功能调往会议室详情页面
            Integer id =Integer.valueOf(request.getParameter("id"));
           String maxnumber=request.getParameter("maxnumber");
            try {
                List<BoardRoomTimeVo> boardRoomTime = boardRoomService.getBoardRoomTime(id);
                request.setAttribute("id",id);
                request.setAttribute("boardRoomTime",boardRoomTime);
                request.setAttribute("maxnumber",maxnumber);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            request.getRequestDispatcher("meetingTime.jsp").forward(request,response);

        }else if("8".equals(flag)){

            Integer id =Integer.valueOf(request.getParameter("id"));
            String start =request.getParameter("start");
            System.out.println(start);
            start=start.replace('T',' ')+":00";
            System.out.println(start);
            String end =request.getParameter("end");
            System.out.println(start);
            end=end.replace('T',' ')+":00";
            System.out.println(end);
            Timestamp tStart=new Timestamp(DateUtil.string3Date(start).getTime());
            Timestamp tEnd=new Timestamp(DateUtil.string3Date(end).getTime());
            System.out.println(id+"**"+tStart+"**"+tEnd);
            List<Meeting> allMeeting = null;
            try {
                allMeeting = meetingService.getAllMeeting(id,tStart, tEnd);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            System.out.println(allMeeting);
            if(allMeeting.size()==0){
                System.out.println(true);
                response.getWriter().write("true");



            }else{
                response.getWriter().write("false");
            }

        }




    }




}
